package com.tengju.bff.interfaces.app.withdraw.facade;

import com.tengju.bff.interfaces.covercharge.constant.PlatformCodeEnum;
import com.tengju.bff.interfaces.covercharge.constant.SignIdentityEnum;
import com.tengju.bff.interfaces.covercharge.dto.BankInfo;
import com.tengju.bff.interfaces.covercharge.dto.WithdrawalModify;
import com.tengju.bff.interfaces.covercharge.intf.IWithdrawalProvider;
import com.tengju.settlement.domain.model.account.Account;
import com.tengju.settlement.domain.model.account.AccountRepository;
import com.tengju.settlement.domain.model.account.AccountType;
import com.tengju.settlement.domain.model.account.UserIdCode;
import com.tengju.user.domain.model.archive.IdCardNo;
import com.tengju.user.domain.model.archive.letax.IdentityType;
import com.tengju.user.domain.model.archive.resume.BankCard;
import com.tengju.user.domain.model.archive.resume.BankCardRepository;
import com.tengju.user.domain.model.archive.resume.BankCardStatus;
import com.tengju.user.domain.model.archive.sign.Sign;
import com.tengju.user.domain.model.archive.sign.SignLog;
import com.tengju.user.domain.model.archive.sign.SignLogRepository;
import com.tengju.user.domain.model.archive.sign.SignRepository;
import com.tengju.user.domain.shared.AccountId;
import java.time.LocalDateTime;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

@Service
public class BankServiceFacade {

    private final AccountRepository accountRepository;
    private final BankCardRepository bankCardRepository;
    private final SignRepository signRepository;
    private final SignLogRepository signLogRepository;
    private final IWithdrawalProvider withdrawalProvider;

    @Autowired
    public BankServiceFacade(AccountRepository accountRepository,
            BankCardRepository bankCardRepository,
            SignRepository signRepository,
            SignLogRepository signLogRepository,
            IWithdrawalProvider withdrawalProvider) {
        this.accountRepository = accountRepository;
        this.bankCardRepository = bankCardRepository;
        this.signRepository = signRepository;
        this.signLogRepository = signLogRepository;
        this.withdrawalProvider = withdrawalProvider;
    }

    @Transactional
    public void sign(com.tengju.user.domain.model.user.UserIdCode idCode, BankCard bankCard,
            Sign sign, boolean history) {

        bankCardRepository.update(idCode, BankCardStatus.UNLINK);

        bankCardRepository.save(bankCard);

        if (sign == null) {
            sign = new Sign(
                    null,
                    idCode,
                    IdentityType.NO_IDENTITY,
                    IdentityType.PERSON,
                    null,
                    "yes",
                    "FIRST",
                    LocalDateTime.now(),
                    null
            );
            signRepository.save(sign);
        } else {
            sign.change(sign.getCurrentIdentity(),
                    IdentityType.PERSON,
                    null,
                    "yes",
                    sign.getIsView()
            );
            signRepository.update(sign.getSignId(), sign);
        }

        SignLog signLog = new SignLog(
                idCode,
                "sign",
                sign.getBeforeIdentity(),
                IdentityType.PERSON,
                "admin"
        );

        signLogRepository.save(signLog);

        if (history) {
            // 修改收款账户
            BankInfo bankInfo = new BankInfo();
            bankInfo.setIdcardName(bankCard.getIdCardName());
            bankInfo.setIdcardNo(bankCard.getIdCardNo().getValue());
            bankInfo.setBankNo(bankCard.getBankNo());
            bankInfo.setBankName(bankCard.getBankName());

            WithdrawalModify withdrawalModify = new WithdrawalModify();
            withdrawalModify.setBankInfo(bankInfo);
            withdrawalModify.setFlag(1);
            withdrawalModify.setCurrentIdentity(SignIdentityEnum.person.getCode());
            withdrawalModify.setIdCode(((Long) idCode.getValue()).intValue());
            withdrawalModify.setPlatformCode(PlatformCodeEnum.DT.getDescriptioin());
            try {
                withdrawalProvider.modifyWithdrawalApply(withdrawalModify);
            } catch (Exception e) {
                throw new RuntimeException("修改收款账户失败", e);
            }
        }
    }

    /**
     * 修改个人提现记录
     * @param idCode
     * @param account
     * @param idCardNo
     * @param idCardName
     * @param bankNo
     * @param bankName
     */
    @Transactional
    public void bankWithdraw(com.tengju.user.domain.model.user.UserIdCode idCode, Account account,
            IdCardNo idCardNo, String idCardName, String bankNo, String bankName) {
        //更新信息
        bankCardRepository.update(idCode, BankCardStatus.UNLINK);
        BankCard bankCard = new BankCard(
                null,
                new AccountId(account.getAccountId().getValue()),
                new com.tengju.user.domain.model.user.UserIdCode(idCode.getValue()),
                idCardNo,
                idCardName,
                bankNo,
                bankName,
                BankCardStatus.ENABLE
        );
        bankCardRepository.save(bankCard);

        // 修改收款账户
        BankInfo bankInfo = new BankInfo();
        bankInfo.setIdcardName(bankCard.getIdCardName());
        bankInfo.setIdcardNo(bankCard.getIdCardNo().getValue());
        bankInfo.setBankNo(bankCard.getBankNo());
        bankInfo.setBankName(bankCard.getBankName());

        WithdrawalModify withdrawalModify = new WithdrawalModify();
        withdrawalModify.setBankInfo(bankInfo);
        withdrawalModify.setFlag(1);
        withdrawalModify.setCurrentIdentity(SignIdentityEnum.person.getCode());
        withdrawalModify.setIdCode(((Long) idCode.getValue()).intValue());
        withdrawalModify.setPlatformCode(PlatformCodeEnum.DT.getDescriptioin());
        try {
            withdrawalProvider.modifyWithdrawalApply(withdrawalModify);
        } catch (Exception e) {
            throw new RuntimeException("修改收款账户失败", e);
        }
    }

    @Transactional
    public void bankWithdraw(com.tengju.user.domain.model.user.UserIdCode idCode,
            IdCardNo idCardNo, String idCardName, String bankNo, String bankName, String withdrawLogId) {
        Account account = accountRepository
                .findOrSave(new UserIdCode(idCode.getValue()), AccountType.DATA_TREE);
        Sign sign = signRepository.find(idCode);
        if (sign.getCurrentIdentity() == IdentityType.PERSON && "yes"
                .equalsIgnoreCase(sign.getIsSigned())) {
            bankCardRepository.update(idCode, BankCardStatus.UNLINK);
            BankCard bankCard = new BankCard(
                    null,
                    new AccountId(account.getAccountId().getValue()),
                    new com.tengju.user.domain.model.user.UserIdCode(idCode.getValue()),
                    idCardNo,
                    idCardName,
                    bankNo,
                    bankName,
                    BankCardStatus.ENABLE
            );
            bankCardRepository.save(bankCard);
        }

        // 修改收款账户
        BankInfo bankInfo = new BankInfo();
        bankInfo.setIdcardName(idCardName);
        bankInfo.setIdcardNo(idCardNo.getValue());
        bankInfo.setBankNo(bankNo);
        bankInfo.setBankName(bankName);

        WithdrawalModify withdrawalModify = new WithdrawalModify();
        withdrawalModify.setWithdrawLogId(Integer.valueOf(withdrawLogId));
        withdrawalModify.setBankInfo(bankInfo);
        withdrawalModify.setFlag(0);
        withdrawalModify.setCurrentIdentity(SignIdentityEnum.person.getCode());
        withdrawalModify.setIdCode(((Long) idCode.getValue()).intValue());
        withdrawalModify.setPlatformCode(PlatformCodeEnum.DT.getDescriptioin());
        try {
            withdrawalProvider.modifyWithdrawalApply(withdrawalModify);
        } catch (Exception e) {
            throw new RuntimeException("修改收款账户失败", e);
        }
    }
}
